﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 活动管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [Authorize]
    public class ActiveinfoController : ControllerBase
    {
        IFreeSql db;
        public ActiveinfoController(IFreeSql _db)
        {
            db = _db;
        }

        /// <summary>
        /// 查询活动管理信息
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">条数</param>
        /// <param name="atitle">活动标题</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfo(int page, int limit, string? atitle)
        {
            var list = db.Select<Models.Activeinfo>()
                .Where(x => ((string.IsNullOrEmpty(atitle)) || (x.Atitle.Contains(atitle))) && x.Isdelete == false)
                .Count(out var total)
                .Page(page, limit)
                .ToList();
            var json = new
            {
                code = 200,
                data = list,
                total = total
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加活动
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromBody] Models.Activeinfo info)
        {
            try
            {
                info.Astate = false;
                info.Isdelete = false;
                int list = db.Insert(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(ex);
            }
        }

        /// <summary>
        /// 修改活动
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update([FromBody] Models.Activeinfo info)
        {
            try
            {
                int list = db.Update<Models.Activeinfo>().SetSource(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除活动
        /// </summary>
        /// <param name="aid">ID</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int aid)
        {
            try
            {
                int list = db.Update<Models.Activeinfo>(aid)
                    .Set(x => x.Isdelete == true)
                    .ExecuteAffrows();
                return Ok(list > 0);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 获取最新的活动信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getNewActiveInfo")]
        public IActionResult getNewActiveInfo()
        {
            var list = db.Select<Models.Activeinfo>().Where(a => a.Astate == false).OrderByDescending(a => a.Creattime).First();
            return Ok(list);
        }

        /// <summary>
        /// 获取活动信息绑定到Echarts报表 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getActiveInfoBindEcharts")]
        [AllowAnonymous]
        public IActionResult getActiveInfoBindEcharts()
        {
            List<Models.Activeinfo> list = db.Select<Models.Activeinfo>().Where(a => a.Isdelete == false).ToList();
            //string[] mouthArr = new string[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
            string[] mouthArr = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };

            int[] numArr = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };



            foreach (Models.Activeinfo item in list)
            {
                string sss = item.Starttime.ToString().Substring(5, 2).Replace('/',' ').Trim();
                string mouth = sss.Length == 1 ? "0" + sss : sss;
                for (int i = 0; i < mouthArr.Length; i++)
                {
                    if (mouth == mouthArr[i])
                    {
                        numArr[i] += 1;
                    }
                }
            }
            return Ok(numArr);

        }
    }
}
